﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class ReportIterationControl : System.Web.UI.UserControl
    {
        public string FromDate = string.Empty;
        public string ToDate = string.Empty;
        public int TasksDue = 0;
        public int TasksClosed = 0;
        public int TasksOpen = 0;
        public decimal HoursEstiatedTotal = 0m;
        public decimal HoursActualTotal = 0m;
        public List<Task> tasks = new List<Task>();

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            List<Task> iterationTasks = new List<Task>();
            List<Task> tasksClosed = new List<Task>();
            List<Task> tasksOpen = new List<Task>();
            Task[] initialTaskList = Task.FindAll(Order.Asc("DueDate"));
            
            foreach (Task t in initialTaskList)
            {
                if (t.DueDate >= Convert.ToDateTime(FromDate) && t.DueDate <= Convert.ToDateTime(ToDate))
                {
                    TasksDue++;
                    if (t.ClosedDateTime.Equals(new DateTime(1900, 1, 1)))
                    {
                        TasksOpen++;
                        tasksOpen.Add(t);
                    }
                    else
                    {
                        TasksClosed++;
                        tasksClosed.Add(t);
                        HoursActualTotal += t.HoursActual;
                        HoursEstiatedTotal += t.HoursEstimated;
                    }
                    
                }
            }
            foreach (Task t in tasksOpen)
            {
                tasks.Add(t);
            }

            foreach (Task t in tasksClosed)
            {
                tasks.Add(t);
            }
            
            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }

        
    }
}